﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using SIA.HwAccess;
using SIA.Configurators;
using SIA.Configurators.ods;
using SIA.HwAccess.PLCSiemens;

namespace SIA.HwMonitors
{
    static class Program
    {
        
        /// <summary>Punto di ingresso principale dell'applicazione.</summary>
        [STAThread]
        static void Main()
        {
            try
            {          
                //Ricerca della dll nella cartella \lib
                //AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
                
                //TagSystem.TagManager tm = new TagSystem.TagManager();
                IDictionaryProperty odsProperty = new OdsDictionaryProperty(".." + System.IO.Path.DirectorySeparatorChar + "etc");

                //IHwDevices devices = new  

                IDictionaryProperty signalsProperty = odsProperty["LISTATAG.ODS"]["SEGNALI ACCUMULO"];
                signalsProperty.Merge(odsProperty["LISTATAG.ODS"]["SEGNALIALLARMI ACCUMULO"]);
                signalsProperty.Merge(odsProperty["LISTATAG.ODS"]["SEGNALI SARMAS"]);
                signalsProperty.Merge(odsProperty["LISTATAG.ODS"]["SEGNALI LINEACESTO"]);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new HwMonitor(new S7PLCs(odsProperty["LISTATAG.ODS"]["DEVICES"], odsProperty["LISTATAG.ODS"]["CONNESSIONI"], signalsProperty)));
            }
            catch ( Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }





        ////This handler is called only when the common language runtime tries to bind to the assembly and fails.            
        //static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        //{       
        //    //Loop through the array of referenced assembly names.
        //    foreach (System.Reflection.AssemblyName strAssmbName in System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies())
        //    {           //Check for the assembly names that have raised the "AssemblyResolve" event.                
        //        if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
        //        {           //Build the path of the assembly from where it has to be loaded.
        //           return System.Reflection.Assembly.LoadFrom("..\\lib\\" + args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll");
        //        }
        //    }
        //    return null;
        //}
    }
}
